package lxt.action;

import java.text.SimpleDateFormat;
import java.util.Date;

import lxt.entity.UserInfo;
import lxt.service.BorrowTruckService;
import lxt.service.InvoiceOrderService;
import lxt.service.QueryDataService;
import lxt.service.UserService;
import lxt.action.core.ActionSupport;
/**
 * 登录管理action
 * @author Wangfd
 *
 */
public class loginAction  extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private UserService userService;
	private InvoiceOrderService invoiceOrderService;
	private BorrowTruckService borrowTruckService;
	private QueryDataService queryDataService;
	
	private String username ;
	private String password ;
	
	/**
	 * 请求位置标示
	 */
	private String f;
	
	/**
	 * 处理登录请求
	 * 验证通过，将用户放入session
	 * @return
	 */
	public String login(){
		/**
		 * 如果是首页跳转,返回登录页面
		 */
		if("login".equals(f)) return INPUT;
		/**
		 * 处理登录逻辑
		 */
		try{
			UserInfo user = userService.isExist(this.username, this.password);
			if(user != null){
				/**
				 * 设置最后一次登陆时间
				 */
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Date date = new Date();
				String temp = sdf.format(date);
				Date lastLogin = sdf.parse(temp);
				user.setLastLogin(lastLogin);
				this.userService.updateUser(user);
				
				this.getSession().put("user", user);
				/**
				 * 查询需要派单的条数
				 */
				String Pnum = String.valueOf(this.invoiceOrderService.loadInvosOrderIdCount(""));
				this.getSession().put("Pnum", Pnum);
				/**
				 * 查询租车未结清数量
				 */
				Integer Bnum = this.borrowTruckService.loadAllBorrowTruckCount("", "", "0");
				this.getSession().put("Bnum", Bnum);
				/**
				 * 查询欠费客户条数
				 */
				Integer Cnum = this.queryDataService.queryOweForPage(null, null, null, null, null, null).size();
				this.getSession().put("Cnum", Cnum);
				return SUCCESS;
			}else{
				if("sessLose".equals(f)){
					this.addActionMessage("Session丢失,请重新登录.");
				}else{
					this.addActionMessage("用户名或者密码错误.");
				}
				return INPUT;
			}
		}catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
	}
	
	/**
	 * 登出请求
	 * @return
	 */
	public String logout(){
		this.getSession().remove("user");
		return SUCCESS;
	}

	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public InvoiceOrderService getInvoiceOrderService() {
		return invoiceOrderService;
	}
	public void setInvoiceOrderService(InvoiceOrderService invoiceOrderService) {
		this.invoiceOrderService = invoiceOrderService;
	}
	public void setF(String f) {
		this.f = f;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public BorrowTruckService getBorrowTruckService() {
		return borrowTruckService;
	}

	public void setBorrowTruckService(BorrowTruckService borrowTruckService) {
		this.borrowTruckService = borrowTruckService;
	}

	public QueryDataService getQueryDataService() {
		return queryDataService;
	}

	public void setQueryDataService(QueryDataService queryDataService) {
		this.queryDataService = queryDataService;
	}

}
